home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5926 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  98 lines

  1. Path: news.ichange.com!newsmaster
  2. From: patrick_widener@mail.amsinc.com (Patrick Widener)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why is a temporary created here?
  5. Date: Wed, 07 Feb 1996 14:12:38 GMT
  6. Organization: American Management Systems, Inc.
  7. Message-ID: <4fac91$eur@ias2.ichange.com>
  8. References: <47ts5e$21o@ams.amsinc.com> <47vvhh$9ce@gabi.gabi-soft.fr>
  9. Reply-To: patrick_widener@mail.amsinc.com
  10. NNTP-Posting-Host: 162.70.176.43
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. kanze@gabi-soft.fr (J. Kanze) wrote:
  14.  
  15. >Patrick Widener (patrick_widener@mail.amsinc.com) wrote:
  16. >
  17. >|> I have this situation:
  18. >
  19. >|> class B
  20. >|> { 
  21. >|> ...
  22. >|> };
  23. >
  24. >|> class DOne : public B
  25. >|> {
  26. >|> ... 
  27. >|> };
  28. >
  29. >|> class DTwo : public B
  30. >|> {
  31. >|> public: 
  32. >|>     void alfa ();
  33. >
  34. >|> protected:
  35. >
  36. >|>     void bravo( B* &);
  37. >|> };
  38. >
  39. >|> void DTwo::alfa
  40. >|> {
  41. >|>     DOne *pDOne;
  42. >|>     DTwo *pDTwo;
  43. >|>     
  44. >|>     bravo (pDOne);  // These lines are flagged by the compiler as 
  45. >|>     bravo (pDTwo); // warnings because temporaries are used for
  46. >|>             // the formal argument of DTwo::bravo()
  47. >|> }
  48. <snip>
  49. >According to the current draft, binding a non-lvalue to a non-const
  50. >reference is illegal, since any changes made through the reference will
  51. >only modify the temporary.  This change was introduced at a later stage
  52. >in the language evolution (about 1990, I think), and most compilers only
  53. >generate a warning, rather than an error, to avoid breaking existing
  54. >code.  But it is still illegal.
  55. >
  56. >From the above, it is not clear what you really want to do.  Perhaps
  57. >declaring the parameter to bravo const would be enough, although in this
  58. >case, since you are dealing with pointers, just pass be value would have
  59. >the same effect.  Alternatively, you could declare pDOne and pDTwo as
  60. >B*.  Or create two bravo functions, one for each type.  Or use an
  61. >explicit temporary of the correct type, and then assign it back to pDOne
  62. >or pDTwo after bravo returns.
  63.  
  64. OK.  Probably more information in the original problem statement would
  65. have helped a little. DTwo actually looks a little more like this:
  66.  
  67. class DTwo : public B
  68. {
  69. public: 
  70.     void alfa ();
  71. protected:
  72.      bool bravo (const Dictionary&, const String&, B* &);
  73.     
  74.     Dictionary DOneDict;
  75.     Dictionary DTwoDict;
  76. };
  77.  
  78. What I actually wanted to do is to use the DTwo::bravo() method to
  79. pick a specified DOne or DTwo pointer out of either DTwo::DOneDict or
  80. DTwo::DTwoDict based on the String that I pass it.  So, to retrieve a
  81. DOne I would call bravo (DOneDict, "key string", pDOne) and likewise
  82. bravo (DTwoDict, "key string 2", pDTwo) to retrieve a particular DTwo.
  83. (Yes, I know this probably isn't the best way to design this).  So,
  84. the need for an lvalue is evident.
  85.  
  86. I thought this was going to work based on the polymorphic behavior of
  87. the pointers.  However, I seem to remember something about parameter
  88. passing being modeled on initialization rather than assignment, so I
  89. would guess that might cause a problem.  oh well.
  90.  
  91. Thanks for responding...
  92.  
  93. --------
  94. patrick widener
  95. patrick_widener@mail.amsinc.com   http://pwidener.amsinc.com:2112
  96. "Ted Striker has more guts in his big toe than most of us have in
  97. our entire large intestine - INCLUDING the colon!!"
  98.